package com.caine.goods.service.impl;

import com.caine.goods.entity.Goods;
import com.caine.goods.mapper.GoodsMapper;
import com.caine.goods.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caine.goods.vo.OrderVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author   V   
 * @since 2021-01-04
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Transactional
    @Override
    public boolean reduceStock(OrderVO vo) {
        Goods goods = getById(vo.getGoodsId());
        BigDecimal divide = goods.getStock().subtract(vo.getCount());
        int ret = divide.compareTo(BigDecimal.valueOf(0));
        if (ret >= 0) {
            goods.setStock(divide);
            goods.updateById();
            return true;
        }
        throw new RuntimeException("库存不足");
    }
}
